﻿using System.Net;
using Telex;

namespace Server1
{
   

    internal class Program
    {
        private static byte[] req = System.Text.Encoding.UTF8.GetBytes("请求=------哈哈哈环境科技");
        private static byte[] res = System.Text.Encoding.UTF8.GetBytes("响应=------一月又一月晕");


        static async Task Main(string[] args)
        {
            var sHost = new ServerBuilder()
                                        .ConfigureSecretkey("11")
                                        .ConfigureChannelActive(OnChannelActive)
                                        .ConfigureChannelInactive(OnChannelInactive)
                                        .AddSendProtocol(10, SendFunc)
                                        .AddCallProtocol(90, CallFunc)
                                        .Build();
            await sHost.BindAsync(IPEndPoint.Parse("127.0.0.1:20001"), new RpcHost("zz", "jianjie"));

            Console.ReadKey();
        }
        
        private static ValueTask OnChannelActive(RpcHost remotehost, ServiceKind kind)
        {
            Console.WriteLine("channel 连接::" + remotehost.ToString() + "        来源种类：" + kind.ToString());
            return ValueTask.CompletedTask;
        }

        private static ValueTask OnChannelInactive(RpcHost remotehost, ServiceKind kind)
        {
            Console.WriteLine("channel 断开::" + remotehost.ToString() + "        来源种类：" + kind.ToString());
            return ValueTask.CompletedTask;
        }

        private static void CallFunc(RpcHost host, uint protocolid, byte[] request, CallResponseDelegate func)
        {
            Console.WriteLine("CallFunc>>收到>" + System.Text.Encoding.UTF8.GetString(request));
            func.Invoke(res, 200);
        }

        private static void SendFunc(RpcHost host, uint protocolid, byte[] request)
        {
            Console.WriteLine("SendFunc>>收到>" + System.Text.Encoding.UTF8.GetString(request));
        }
    }
}
